/*
 * EXTInterrupt.c
 *
 *  Created on: Jun 17, 2024
 *      Author: 17850
 */

#include "stm32l4xx_hal.h"
#include "EXTInterrupt.h"
#include "spi.h"
#include <stdio.h>

void GPIO_EXTInterrupt_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    // 开启PB时钟和I/O复用时钟
    RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;

    GPIO_InitStructure.Pin = GPIO_PIN_4; // 改为PB5
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void EXTInterrupt_Mode_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  // 配置GPIOA的Pin 3引脚作为外部中断输入
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  // 配置外部中断线
  HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}





void EXTInterrupt_Init(void)
{
    GPIO_EXTInterrupt_Configuration();
    EXTInterrupt_Mode_Configuration();
}

extern uint8_t flog;
extern uint32_t ch1_data;
extern uint32_t ch2_data;
extern uint16_t point_cnt;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == GPIO_PIN_4)
    {
        // 处理PA3引脚的中断事件


    	uint8_t j;
    	uint8_t read_data[9];



    	  for (j = 0; j < 9; j++)		// 连续读取9个数�?
    	  {
    		  read_data[j] = SPI3_ReadWriteByte(0xFF);
    	  }

    	  ch1_data = 0;
    	  ch2_data = 0;
    	  ch1_data |= (uint32_t)read_data[3] << 16;
    	  ch1_data |= (uint32_t)read_data[4] << 8;
    	  ch1_data |= (uint32_t)read_data[5] << 0;
    	  ch2_data |= (uint32_t)read_data[6] << 16;
    	  ch2_data |= (uint32_t)read_data[7] << 8;
    	  ch2_data |= (uint32_t)read_data[8] << 0;

    	  point_cnt++;
    	  flog = 1;
    }
}




